package com.honghu.villagepost.dao.data.database;

import android.database.sqlite.SQLiteDatabase;

import java.util.Locale;

/**
 * Created by liyu
 */

public abstract class DatabaseUpgrade {

    public static final String TAG = DatabaseUpgrade.class.getSimpleName();

    private int mUpgradeVersion;

    public DatabaseUpgrade(int upgradeVersion) {
        mUpgradeVersion = upgradeVersion;
    }

    public void checkUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < mUpgradeVersion && mUpgradeVersion <= newVersion) {
            doUpgrade(db, oldVersion, newVersion);
        }
    }

    protected abstract void doUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);

    /**
     * 表添加字段
     *
     * @param table
     * @param columnName
     * @param dataType
     * @return
     */
    protected String alterTableAddSelection(String table, String columnName, String dataType) {
        return String.format(Locale.getDefault(), "ALTER TABLE %s Add %s %s", table, columnName, dataType);
    }
}
